package com.dpteam.controller;

import com.dpteam.bean.UserInfo;
import com.dpteam.service.UserService;
import com.dpteam.util.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FileUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/11
 * Time: 10:09
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Resource
    private UserService userService;

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    /**
     *
     * @param type
     * @param map
     * @return
     */
    @RequestMapping("/inputMessage/{type}")
    public String linkInputMessage(@PathVariable("type") int type, ModelMap map) {
        logger.info("linkInputMessage type:[{}]", type);
        if (type == 1) {
            map.put("userType", 1);
        } else if (type == 2) {
            map.put("userType", 2);
        } else if (type == 3) {
            map.put("userType", 3);
        }
        return "login/inputMessage";
    }

    /**
     *
     * @param name
     * @param userType
     * @param password
     * @param rememberMe
     * @param map
     * @return
     */
    @RequestMapping("login")
    public String login(String name, int userType, String password, String rememberMe, ModelMap map) {
        logger.info("login: [name:{},userType:{},password:{},rememberMe:{}]", name, userType, password, rememberMe);
        //TODO 参数验证
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        token.setRememberMe(true);
        Subject currentUser = SecurityUtils.getSubject();
        String loginMessage = "用户名或密码错误";
        try {
            logger.info("对用户[{}]进行登录验证...验证开始");
            currentUser.login(token);
            logger.info("对用户[{}]进行登录验证...验证通过");
        } catch (AuthenticationException e) {
            logger.error("登录验证失败", e);
            map.put(Constant.loginFailMessage, loginMessage);
        }
        UserInfo userInfo = (UserInfo)SecurityUtils.getSubject().getSession().getAttribute(Constant.currentUserKey);
        if (currentUser.isAuthenticated()) {
            if (userInfo.getRoleId() == 1) {
                if (userType == 1) {
                    return "managerSystem";
                } else if (userType == 2) {
                    return "storageSystem";
                } else if (userType == 3) {
                    return "checkOutSystem";
                }
            }

            //登录成功
            if (userType == 2 && 3 == userInfo.getRoleId()) {
                return "storageSystem";
            } else if (userType == 3 && 2 == userInfo.getRoleId()) {
                return "checkOutSystem";
            }
        }
        token.clear();
        map.put("userType", userType);
        map.put("error","用户名或密码错误");
        map.put("name",name);
        map.put("password",password);
        return "login/inputMessage";
    }

    /**
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout() {
        userService.logout();
        return "redirect:/";
    }

}
